home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BTCLOSE.C < prev    next >
Text File  |  1991-09-23  |  2KB  |  87 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btclose.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STRING
  11. #include <string.h>
  12. #endif
  13.  
  14. /* library headers */
  15. #include <blkio.h>
  16.  
  17. /* local headers */
  18. #include "btree_.h"
  19.  
  20. /*man---------------------------------------------------------------------------
  21. NAME
  22.      btclose - close btree
  23.  
  24. SYNOPSIS
  25.      #include <btree.h>
  26.  
  27.      int btclose(btp)
  28.      btree_t *btp;
  29.  
  30. DESCRIPTION
  31.      The btclose function causes any buffered data for the named btree
  32.      to be written out, the file unlocked, and the btree to be closed.
  33.  
  34.      btclose will fail if one or more of the following is true:
  35.  
  36.      [EINVAL]       btp is not a valid btree pointer.
  37.      [BTENOPEN]     btp is not open.
  38.  
  39. SEE ALSO
  40.      btcreate, btlock, btopen, btsync.
  41.  
  42. DIAGNOSTICS
  43.      Upon successful completion, a value of 0 is returned.  Otherwise,
  44.      a value of -1 is returned, and errno set to indicate the error.
  45.  
  46. ------------------------------------------------------------------------------*/
  47. #ifdef AC_PROTO
  48. int btclose(btree_t *btp)
  49. #else
  50. int btclose(btp)
  51. btree_t *btp;
  52. #endif
  53. {
  54.     /* validate arguments */
  55.     if (!bt_valid(btp)) {
  56.         errno = EINVAL;
  57.         return -1;
  58.     }
  59.  
  60.     /* check if not open */
  61.     if (!(btp->flags & BTOPEN)) {
  62.         errno = BTENOPEN;
  63.         return -1;
  64.     }
  65.  
  66.     /* synchronize file with buffers and unlock file */
  67.     if (btlock(btp, BT_UNLCK) == -1) {
  68.         BTEPRINT;
  69.         return -1;
  70.     }
  71.  
  72.     /* free memory allocated for btree */
  73.     bt_free(btp);
  74.  
  75.     /* close btree file */
  76.     if (bclose(btp->bp) == -1) {
  77.         BTEPRINT;
  78.         return -1;
  79.     }
  80.  
  81.     /* scrub slot in btb table then free it */
  82.     memset(btp, 0, sizeof(*btb));
  83.     btp->flags = 0;
  84.  
  85.     return 0;
  86. }
  87.